<?
    function getDerivation($xml_file) {
        global $derivate;
        //$objeto  = domxml_open_file($xml_file);
        //$raiz = $objeto->document_element();
        $objeto = new DOMDocument();
        $objeto->load($xml_file);
        $filho = $objeto->getelementsbytagname("derivation");
        if ($filho->length > 0) {
            $derivate = $filho->item(0)->getelementsbytagname("derivate");
            return $derivate;
        }
        else return null;
    }

    function getIValue($indice) {
        global $derivate, $value;
        $ivalue = $derivate->item($indice)->getelementsbytagname("initial_value");
        if ($ivalue->length > 0) {
            $value = $ivalue->item(0)->nodeValue;
            return $value;
        }
        else return null;
    }

    function getMaxIter($indice) {
        global $derivate, $miter;
        $maxiter = $derivate->item($indice)->getelementsbytagname("maxiter");
        if ($maxiter->length > 0) {
            $miter = $maxiter->item(0)->nodeValue;
            return $miter;
        }
        else return null;
    }

    function getError($indice) {
        global $derivate, $error;
        $evalue = $derivate->item($indice)->getelementsbytagname("error");
        if ($evalue->length > 0) {
            $error = $evalue->item(0)->nodeValue;
            return $error;
        }
        else return null;
    }

    function getFunction($indice) {
        global $derivate, $f;
        $function = $derivate->item($indice)->getelementsbytagname("function");
        if ($function->length > 0) {
            $f = $function->item(0)->nodeValue;
            return $f;
        }
        else return null;
    }

    function getDesc($indice) {
        global $derivate, $desc;
        $descricao = $derivate->item($indice)->getelementsbytagname("description");
        $desc = $descricao->item(0)->nodeValue;
        return $desc;
    }

    function getCom($indice) {
        global $derivate, $com;
        $comentario = $derivate->item($indice)->getelementsbytagname("comments");
        if ($comentario->length > 0) {
            $com = $comentario->item(0)->nodeValue;
            return $com;
        }
        else return null;
    }

    function getRef($indice) {
        global $derivate, $ref;
        $referencia = $derivate->item($indice)->getelementsbytagname("reference");
        if ($referencia->length > 0) {
            $ref = $referencia->item(0)->nodeValue;
            return $ref;
        }
        else return null;
    }

    function createMenu() {
        global $derivate,$msg;
        for($i = 0; $i < $derivate->length; $i++) {
            echo "<a class=bibliotecaexemplos href=javascript:createLinks($i)><u>";
            $temp = getFunction($i); $temp1 = $i+1;
            echo $msg->get('XML_EXIBE_DERIVADA_MSG01')." "."$temp1".": &nbsp f(x) = ";
            echo "$temp";
            echo "</u></a><p>";
        }
        return;
    }
    if (empty($xml_file) && empty($id_bib)) $id_bib="bib_demo.xml"; 
    if ( (strlen(@$indice)==0) || ($indice == null) ) { ?>
    <p>
        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function createLinks(value) {
               document.myform.indice.value = value;
               document.myform.action = 'index.php?inc=derivada.php&modo=exemplo';
               document.myform.submit();
            }

        </script>
    <? if (isset($id_bib)) $xml_file = $DOCUMENT_ROOT."/bibliotecas/".$id_bib; ?>
        <? verificaBib(@$xml_file); ?>
        <?  if (file_exists($xml_file)) {
                $temp = getDerivation($xml_file);
                if (!$temp==null) {
                   echo "<p>";
                   echo "<p align=left><font face=Arial>".$msg->get('XML_EXIBE_MSG04')."</font>";
                   echo "<p>";
                   createMenu();
                } else {
                     echo $msg->get('XML_EXIBE_MSG05');
                     echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
                }
        } else {
                 echo $msg->get('XML_EXIBE_MSG06');
                 echo "<p><input class=button type=button value=".$msg->get('XML_BACK')." onClick=history.back()>";
        }
 	 ?>

        <input type="hidden" name="id_bib" value="<? echo $id_bib ?>">
        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="indice">
        <input class="button" type="button" value="<?php echo $msg->get('XML_EXIBE_MSG07') ?>" onClick="history.back()">
        </form>

<? } else { ?>
<p>

        <form name="myform" enctype="multipart/form-data" method="post">

        <script language="javascript" type="text/javascript">
            function sendToCalculus() {

               document.myform.action = 'index.php?id_bib=<?echo $id_bib?>&indice=<?echo $indice?>&inc=derivada.php&modo=exemplo';
               document.myform.submit();
            }

        </script>

        <? getDerivation($xml_file); ?>
        <? $funcao = getFunction($indice);?>
        <? $x = getIValue($indice);?>
        <? $k = getMaxIter($indice);?>
        <? $erro = getError($indice);?>
        <? $desc = getDesc($indice); ?>
        <? $com = getCom($indice); ?>
        <? $ref = getRef($indice); ?>

        <? verificaBib(@$xml_file); ?>
        <? if (!strlen($desc) == 0) { ?>
              <p align="left"><font face="Arial"><b><?php echo $msg->get('Descricao') ?>: </b><? echo htmlentities(utf8_decode($desc)) ?></font>
        <? } ?>
        
 		<p align="left"><font face="Arial"><?php echo $msg->get('XML_EXIBE_DERIVADA_MSG04') ?></font>
		<p align="left"><font face="Arial">f(x) = </font>
	   	<input type="text" size='60' maxlength='100' name="funcao" value="<? echo $funcao ?>">
		<a href=javascript:winopen('ajuda/funcoesphp.php?idioma=PT_BR.php')><font face="Arial">>><u><?php echo $msg->get('XML_EXIBE_DERIVADA_MSG05') ?></u><<</font></a>
		<p>
  		<p align="left"><font face="Arial"><?php echo $msg->get('XML_EXIBE_DERIVADA_MSG06') ?></font>
           	<input type="text" size='5' maxlength='5' name="x" value="<?echo $x?>">
		<p>
  		<p align="left"><font face="Arial"><?php echo $msg->get('XML_EXIBE_DERIVADA_MSG07') ?> </font>
           	<input type="text" size='5' maxlength='5' name="k" value="<?echo $k?>">
		<p>
  		<p align="left"><font face="Arial"><?php echo $msg->get('XML_EXIBE_DERIVADA_MSG08') ?> </font>
           	<input type="text" size='5' maxlength='5' name="erro" value="<?echo $erro?>">
		<p>
        <? if (!$com == null) { ?>
              <p align="left"><font face="Arial"><b><?php echo $msg->get('XML_EXIBE_MSG08') ?> </b><? echo htmlentities(utf8_decode($com)) ?></font>
        <? } ?>
        <p>
        <? if (!$ref == null) { ?>
              <p align="left"><font face="Arial"><b><?php echo $msg->get('XML_EXIBE_MSG09') ?> </b><? echo htmlentities(utf8_decode($ref)) ?></font>
        <? } ?>
        <p>

        <input type="hidden" name="xml_file" value="<? echo $xml_file ?>">
        <input type="hidden" name="Calcular" value="sim">
        <input type="hidden" name="navegacao" value="sim">
        <input class="button" type="button" value=<? echo $msg->get('XML_EXIBE_MSG010'); ?> onClick="sendToCalculus()"><p>
        <input class="button" type="button" value=<?php echo $msg->get('XML_EXIBE_MSG011') ?> onClick="history.back()">
        </form>
	<?
	echo "<form name=\"exemplos\" enctype=\"multipart/form-data\" method=\"post\">";
	echo "<script language=\"javascript\" type=\"text/javascript\">\n";
	echo "function criarLinks(value) {\n";
	echo "document.exemplos.indice.value = value;\n";
	echo "document.exemplos.action = 'index.php?inc=derivada.php&modo=exemplo'\n";
	echo "document.exemplos.submit();\n";
	echo "}\n";
	echo "</script>";
	echo "<input type=\"hidden\" name=\"id_bib\" value=\"".$id_bib."\">";
	echo "<input type=\"hidden\" name=\"xml_file\" value=\"".$xml_file."\">";
	echo "<input type=\"hidden\" name=\"indice\">";
	echo "</form>";
	echo $msg->get('Outros_Exemplos')."<br>";
	//$objeto  = domxml_open_file($xml_file);
	//$raiz = $objeto->document_element();
    $objeto = new DomDocument();
    $objeto->load($xml_file);
	$filho = $objeto->getelementsbytagname("derivation");
	if ($filho->length > 0)
	{
		$derivate = $filho->item(0)->getelementsbytagname("derivate");
		for ($i=0; $i<$derivate->length; $i++)
		{
			if ($i != 0) echo " | ";
			if ($i == $indice) echo "<b>";
			echo "<a href=\"javascript:criarLinks($i)\"><u>".($i+1)."</u></a>";
			if ($i == $indice) echo "</b>";
		}
	}
	?>

<? } ?>
